using System;
using System.Collections.Generic;
using System.Text;
using iGrabber.com.amazon.webservices;

namespace iGrabber
{
    class AmazonSearch
    {
        private static AWSECommerceService amazonService;
        private const bool HTMLFilteringEnabled = true;

        public static string SearchCDs(string searchString)
        {
            ItemSearchRequest itemSearchRequest = new ItemSearchRequest();
            ItemSearch itemSearch = new ItemSearch();

            itemSearchRequest.Keywords = searchString;
            itemSearchRequest.ResponseGroup = new string[1] { "Medium" };
            itemSearchRequest.SearchIndex = "Music";

            itemSearch.SubscriptionId = "0525E2PQ81DD7ZTWTK82";
            itemSearch.Request = new ItemSearchRequest[1] { itemSearchRequest };

            amazonService = new AWSECommerceService();
            amazonService.Url = "http://soap.amazon.com/onca/soap?Service=AWSECommerceService";

            ItemSearchResponse response = new ItemSearchResponse();
            Item[] items;

            try
            {
                response = amazonService.ItemSearch(itemSearch);
            }
            catch (Exception ex)
            {
                response = null;
                Program.WriteLog("Exception caught.");
                Program.WriteLog(ex.Message);
            }

            if (response != null)
            {
                items = response.Items[0].Item;

                if (items == null)
                {
                    return "";
                }

                return AmazonItemLargeImageURLToString(items[0]);
            }

            return "";
        }

        public static List<string[]> ManualSearchCDs(string searchString)
        {
            ItemSearchRequest itemSearchRequest = new ItemSearchRequest();
            ItemSearch itemSearch = new ItemSearch();

            itemSearchRequest.Keywords = searchString;
            itemSearchRequest.ResponseGroup = new string[1] { "Medium" };
            itemSearchRequest.SearchIndex = "Music";

            itemSearch.SubscriptionId = "0525E2PQ81DD7ZTWTK82";
            itemSearch.Request = new ItemSearchRequest[1] { itemSearchRequest };

            amazonService = new AWSECommerceService();
            amazonService.Url = "http://soap.amazon.com/onca/soap?Service=AWSECommerceService";

            ItemSearchResponse response = new ItemSearchResponse();
            Item[] items;

            try
            {
                response = amazonService.ItemSearch(itemSearch);
            }
            catch (Exception ex)
            {
                Program.WriteLog("Exception caught.");
                Program.WriteLog(ex.Message);
                return null;
            }

            if (response != null)
            {
                items = response.Items[0].Item;

                if (items == null)
                {
                    return null;
                }

                List<string[]> resultList = new List<string[]>();

                for (int i = 0; i < items.Length; i++)
                {
                    string[] resultString = new string[2];
                    resultString[0] = items[i].ItemAttributes.Title;
                    resultString[1] = AmazonItemLargeImageURLToString(items[i]);

                    if (!resultString[1].Equals(""))
                    {
                        resultList.Add(resultString);
                    }
                }

                return resultList;
            }

            return null;
        }


        private static string AmazonItemLargeImageURLToString(Item item)
        {
            string largeImageURL = "";

            if (item.LargeImage != null)
            {
                largeImageURL = item.LargeImage.URL;
            }
            if (largeImageURL.Equals("") && item.MediumImage != null)
            {
                largeImageURL = item.MediumImage.URL;
            }
            return largeImageURL;
        }
    }
}
